﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TagCore
{
    public class FileData
    {
        private string friendlyName;
        private string extension;
        private string path = string.Empty;

        public FileData(string file)
        {
            SplitFileName(file);
        }

        public void Rename(string newFileName)
        {
            // new friendly name
            string newFile = path + "\\" + newFileName + GetExtension();
            System.IO.File.Move(File, newFile);
            friendlyName = newFileName;
        }

        public DateTime LastModified
        {
            get
            {
                return System.IO.File.GetLastWriteTime(File);
            }
        }

        public string File
        {
            get
            {
                return GetFullFilePath();
            }
        }

        private string GetExtension()
        {
            return extension.Length > 0 ? "." + extension : "";
        }

        private string GetFullFilePath()
        {
            return path + "\\" + friendlyName + GetExtension();
        }

        public string Filename
        {
            get
            {
                return friendlyName;
            }
        }

        public string Extension
        {
            get
            {
                return extension;
            }
        }

        private void SplitFileName(string file)
        {
            // get last backslash
            int index = file.LastIndexOf('\\');
            if (index > 0)
            {
                string filename = file.Substring(index + 1);
                path = file.Substring(0, index);
                SplitExtension(filename);
            }
            else
            {
                SplitExtension(file);
            }
        }

        private void SplitExtension(string filename)
        {
            int index = filename.LastIndexOf('.');
            if (index > 0)
            {
                extension = filename.Substring(index + 1);
                friendlyName = filename.Substring(0, index);
            }
            else
            {
                friendlyName = filename;
            }
        }
    }
}
